package com.aim.controller.system;

import com.aim.common.annotion.BizLog;
import com.aim.common.annotion.Permission;
import com.aim.common.base.AjaxResult;
import com.aim.common.constant.dictmap.NoticeDict;
import com.aim.common.handler.log.LogObjectHolder;
import com.aim.common.util.*;
import com.aim.config.shiro.ShiroKit;
import com.aim.entity.system.SysNotice;
import com.aim.service.system.SysNoticeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @AUTO 系统消息控制器
 * @Author AIM
 * @DATE 2019/4/25
 */
@Controller
@RequestMapping("/notice")
public class SysNoticeController {

    private static String PREFIX = "system/notice/";

    @Autowired
    private SysNoticeService noticeService;

    /**
     * 列表页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "list.html";
    }

    /**
     * 查询列表
     */
    @PostMapping("/list")
    @ResponseBody
    public Object list(HttpServletRequest request) {
        Map<String, Object> params = WebUtil.getRequestMap(request);
        PageHelper.startPage(PageUtil.getPageNum(params), PageUtil.getPageSize(params));
        List<SysNotice> list = noticeService.findList(params);
        PageInfo<SysNotice> pageInfo = new PageInfo<SysNotice>(list);
        return AjaxResult.success((int) pageInfo.getTotal(), list);
    }

    /**
     * 添加编辑页面
     */
    @BizLog(value = "添加编辑消息", key = "noticeId", dict = NoticeDict.class)
    @Permission
    @RequestMapping(value = {"/add", "/update/{id}"})
    public String editView(@PathVariable(required = false) String id, Model model) {
        if (OftenUtil.isNotEmpty(id)) {
            SysNotice notice = noticeService.get(ConverterUtils.toLong(id));
            model.addAllAttributes(BeanUtils.javaMap(notice));
            LogObjectHolder.me().set(notice);
        }
        return PREFIX + "edit.html";
    }

    /**
     * 保存
     */
    @Permission
    @PostMapping("/save")
    @ResponseBody
    public Object save(SysNotice notice) {
        SysNotice sysNotice = this.noticeService.get(notice.getNoticeId());
        if (null == sysNotice) {
            notice.setCreateTime(new Date());
            notice.setCreateUser(ShiroKit.getUser().getUserId());
            this.noticeService.save(notice);
        } else {
            BeanUtils.copyProperties(notice, sysNotice);
            sysNotice.setUpdateTime(new Date());
            sysNotice.setUpdateUser(ShiroKit.getUser().getUserId());
            this.noticeService.update(sysNotice);
        }
        return AjaxResult.success();
    }

    /**
     * 删除
     *
     * @param ids 删除ID集合
     */
    @Permission
    @PostMapping("/del")
    @ResponseBody
    public Object del(String ids) {
        if (OftenUtil.isNotEmpty(ids)) {
            this.noticeService.batchDelete(ids.split(","));
            return AjaxResult.success();
        } else {
            return AjaxResult.error("参数异常");
        }
    }

    /**
     * 跳转到首页通知
     */
    @RequestMapping("/hello")
    public String hello() {
        List<SysNotice> notices = noticeService.findList();
        WebUtil.setAttr("noticeList", notices);
        return PREFIX + "notice_index.html";
    }
}
